<template>
  <div>
    <van-nav-bar title="个人信息" left-text="返回" left-arrow @click-left="onClickLeft" />
    <van-form @submit="onSubmit">
      <van-cell-group inset>
        <van-field
          v-model="oldPassword"
          type="oldPassword"
          name="oldPassword"
          label="旧密码"
          placeholder="旧密码"
          :rules="[{ required: true}]"
        />
        <van-field
          v-model="newPassword"
          type="newPassword"
          name="newPassword"
          label="新密码"
          placeholder="新密码"
          :rules="[{ required: true, message: '请填写新密码' }]"
        />
        <van-field
          v-model="newPassword2"
          type="newPassword2"
          name="newPassword2"
          label="新密码"
          placeholder="新密码"
          :rules="[{ required: true, message: '请填写新密码' }]"
        />
      </van-cell-group>
      <div style="margin: 16px;">
        <van-button round block type="primary" native-type="submit">提交</van-button>
      </div>
    </van-form>
  </div>
</template>

<script setup>
import { ref, reactive } from "vue";
import { useRouter } from "vue-router";
import { amendpwd } from "../model/userModel";
let router = useRouter();

const oldPassword = ref("");
const newPassword = ref("");
const newPassword2 = ref("");
let data = reactive({
  user: {}
});
const onSubmit = values => {
  if (values.oldPassword !== values.newPassword) {
    if (values.newPassword == values.newPassword2) {
      console.log("submit", values);
      console.log(values);
      amendpwd(
        {
          newPassword: values.newPassword,
          oldPassword: values.oldPassword
        },
        {
          Authorization: window.localStorage.getItem("token")
        }
      ).then(res => {
        console.log(res);
      });
    } else {
      alert("您两次输入的新密码不一致");
    }
  } else {
    alert("新密码不能和旧密码相同");
  }
};

const onClickLeft = () => {
  router.push("/edituser");
};
</script>

<style>
</style>